package cn.gov.msa.util;

import java.util.HashSet;
import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {
	/**
	 * 获取用户认证后json中的信息
	 * @param data 用户认证的json数据
	 * @param key  
	 * @return
	 */
	public static Object getJsonUser(JSONObject data, String key){
		return data.getJSONObject("datas").getJSONObject("table").getJSONObject("row").get(key);
	}
	
	/**
	 * 根据用户和appid获取角色信息
	 * @param data 用户认证的json数据
	 * @return
	 */
	public static String getJsonRoles(JSONObject data){
		String roleNames="";
		JSONArray jArray = data.getJSONObject("datas").getJSONObject("table").getJSONArray("row");
		for (int i=0; i< jArray.size(); i++) {
			JSONObject table = JSONObject.fromObject(jArray.get(i));
			String roleName = (String)table.get("roleName");
			roleName = roleName.trim();
			roleNames=roleNames+roleName+",";
		}
		return roleNames;
	}
	
	/**
	 * 从全部角色信息中获取app=100的角色信息
	 * @param data 用户认证的json数据
	 * @return
	 */
	public static String getJsonRolesFromAll(JSONObject data){
		String roleNames="";
		JSONArray jArray = data.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table");
		for (int i=0; i< jArray.size(); i++) {
			JSONObject table = JSONObject.fromObject(jArray.get(i));
			String  tableName= (String)table.get("tableName");
			if("role".equals(tableName)){
				JSONArray jsonArray = table.getJSONArray("row");
				for (Object object : jsonArray) {
					JSONObject fromObject = JSONObject.fromObject(object);
					String appId = (String)fromObject.get("appId");
					if("100".equals(appId)){
						String roleName = (String)fromObject.get("roleName");
						roleName = roleName.trim();
						roleNames=roleNames+roleName+",";
					}
				}
			}
		}
		return roleNames;
	}
	
	
	
	/**
	 * 获取用户认证后json中组织机构code
	 * @param data 用户认证的json数据
	 * @return
	 */
	public static String getJsonOrgCode(JSONObject data){
		String orgCode="";
		JSONArray jArray = data.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table");
		for (int i=0; i< jArray.size(); i++) {
			JSONObject table = JSONObject.fromObject(jArray.get(i));
			String  tableName= (String)table.get("tableName");

		if("org".equals(tableName)){
				JSONArray jsonArray = table.getJSONArray("row");
				for (Object object : jsonArray) {
					JSONObject fromObject = JSONObject.fromObject(object);
					orgCode = (String)fromObject.get("orgCode");
				}
				
			}
		}
		return orgCode;
	}
	
	/**
	 * 去除简单JSONArray每个JSON对象的value为空为null的key
	 * @param array  JSONArray:[{"key1":"","key2":"3"},{"key4":null,"key2":"value","key1":"","key3":""},{"key3":null}]
	 * @return JSONArray:[{"key2":"3"},{"key2":"value"}]
	 */
	public static void removeEmptyObject(JSONArray array) {
		for (int i = 0; i < array.size(); i++) {
			JSONObject temp=array.getJSONObject(i);
			HashSet<String> keys=new HashSet<String>();
			Iterator iterator = temp.keys(); 
			while(iterator.hasNext()) {
				String key=(String) iterator.next();
				if(null==temp.getString(key)||"".equals(temp.getString(key))||"null".equals(temp.getString(key))) {
					keys.add(key);
				}	
			} 
			for (String key : keys) {  
				temp.remove(key);
			}  
		}
		for (int i = 0; i < array.size(); i++) {
			JSONObject temp=array.getJSONObject(i);
			if(temp.isNullObject()||temp.isEmpty()) {
				array.discard(i);
				i--;
			}
		}
	}
}
